<template>
  <div class="login-container">
    <div class="login-main">
      <div class="login-left">
        <img :src="logoSrc">
      </div>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        auto-complete="on"
        class="login-form"
        label-position="left"
      >
        <div class="title">出入库管理系统</div>
        <el-form-item prop="username">
          <img :src="light" class="light">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.userName"
            auto-complete="on"
            name="username"
            placeholder="Username"
            tabindex="1"
            type="text"
          />
        </el-form-item>

        <el-form-item prop="password">
          <img :src="light" class="light2">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            auto-complete="on"
            name="password"
            placeholder="Password"
            tabindex="2"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>

        <div v-if="!loading" class="btn login-btn" @click="handleLogin">
          登 录
        </div>
        <div v-else class="btn login-btn-active">登 录 中···</div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import md5 from 'js-md5'
import { mapGetters } from 'vuex'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入用户名！'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error('密码格式错误！'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        userName: '',
        password: ''
      },
      logoSrc: require('@/assets/login/title.png'),
      light: require('@/assets/login/light.png'),
      loginRules: {
        userName: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleJump(url) {
      window.open(url, '_blank')
    },
    handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          this.loading = true
          const result = await this.$store.dispatch(
              'user/login',
              this.loginForm
          )
          if (result.user && result.user.userName) {
            this.$router.push({
              path: '/dashboard'
            })
          } else {
            this.$message.error('获取用户信息失败，请重新登录！')
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #047a3d;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 50px;
    width: 425px;
    background: url(~@/assets/login/input-bg.png) no-repeat;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 40px;
      color: $light_gray;
      height: 50px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #047a3d;
$dark_gray: #fff;
$light_gray: #eee;

.login-container {
  background: url(~@/assets/login/bj.jpg) no-repeat;
  background-size: cover;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;

  .login-main {
    display: flex;
    margin: 100px 0 0 0;
    flex-direction: column;

    .login-left {
      width: 100%;
      display: flex;
      flex-direction: column;
      align-items: center;

      .intro-imgs {
        position: relative;
        width: 642px;
        height: 529px;
        background: url(~@/assets/login/illustrator.png) no-repeat;
        margin-top: 80px;

      }
    }

    .login-form {
      position: relative;
      width: 545px;
      max-width: 100%;
      padding: 60px 60px 0;
      margin: 100px auto;
      height: 450px;
      overflow: hidden;
      border-radius: 20px;
      background: rgba(1, 121, 60, 0.4);

      .title {
        font-size: 28px;
        font-weight: bold;
        color: #fff;
        margin-bottom: 40px;
        width: 100%;
        text-align: center;
      }
    }

    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      position: absolute;
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .light {
      position: absolute;
      top: -34px;
      left: 80px;
    }

    .light2 {
      position: absolute;
      bottom: -22px;
      right: 80px;
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }

    .btn {
      margin-top: 50px;
      width: 100%;
      height: 49px;
      box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
      border-radius: 4px;
      line-height: 49px;
      color: #fff;
      font-size: 18px;
      text-align: center;
      font-weight: bold;
      cursor: pointer;
      user-select: none;
      background-image: linear-gradient(
              to bottom,
              rgba(4, 122, 61, 0.6),
              rgba(4, 122, 61, 0.7),
              rgba(4, 122, 61, 0.8),
              rgba(4, 122, 61, 1)
      );
    }

    .login-btn {
      &:active {
        animation: showBtn 0.5s infinite;
        animation-fill-mode: forwards;
      }
    }
  }

  .login-btn-active {
    animation: showBtn 0.5s infinite;
    animation-fill-mode: forwards;
  }

}
</style>
